﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using ThoughtWorks.QRCode.Codec;

namespace QRCode
{
	public class QRCodeHandler
	{
		public bool CreateQRCode(string QRString, string QRCodeEncodeMode, short QRCodeScale, int QRCodeVersion, string QRCodeErrorCorrect, string filePath, bool hasLogo, string logoFilePath)
		{
			bool result = true;
			QRCodeEncoder qRCodeEncoder = new QRCodeEncoder();
			if (QRCodeEncodeMode != null)
			{
				if (QRCodeEncodeMode == "Byte")
				{
					qRCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
					goto IL_64;
				}
				if (QRCodeEncodeMode == "AlphaNumeric")
				{
					qRCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
					goto IL_64;
				}
				if (QRCodeEncodeMode == "Numeric")
				{
					qRCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
					goto IL_64;
				}
			}
			qRCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
		IL_64:
			qRCodeEncoder.QRCodeScale = (int)QRCodeScale;
			qRCodeEncoder.QRCodeVersion = QRCodeVersion;
			if (QRCodeErrorCorrect != null)
			{
				if (QRCodeErrorCorrect == "L")
				{
					qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
					goto IL_E9;
				}
				if (QRCodeErrorCorrect == "M")
				{
					qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
					goto IL_E9;
				}
				if (QRCodeErrorCorrect == "Q")
				{
					qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
					goto IL_E9;
				}
				if (QRCodeErrorCorrect == "H")
				{
					qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
					goto IL_E9;
				}
			}
			qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
			//try
			//{
			IL_E9:
				Image image = qRCodeEncoder.Encode(QRString, Encoding.UTF8);
				FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
				image.Save(fileStream, ImageFormat.Jpeg);
				fileStream.Close();
				if (hasLogo)
				{
					Image image2 = Image.FromFile(logoFilePath);
					Graphics graphics = Graphics.FromImage(image);
					int x = image.Width / 2 - image2.Width / 2;
					int y = image.Height / 2 - image2.Height / 2;
					graphics.DrawImage(image2, new Rectangle(x, y, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel);
					graphics.Dispose();
					image.Save(filePath);
					image2.Dispose();
				}
				image.Dispose();
			//}
			//catch (Exception var_8_1B3)
			//{
			//	result = false;
			//}
			return result;
		}
	}
}
